﻿@using BootstrapBlazor.Shared.Samples;
@inject IStringLocalizer<Textareas> Localizer
@implements IDisposable

<Textarea class="mb-3" placeholder="@Localizer["TextareasPlaceHolder"]" rows="10" @ref="TextareaElement" @bind-Value="@Text" IsAutoScroll="IsAutoScroll" />

<BootstrapInputGroup>
    <Button Text="@Localizer[ChatLocalizerName]" OnClick="MockChat" Icon="fa-fw fas fa-comments" />
    <Button Text="@Localizer["TextareasScrollToTop"]" OnClick="ScrollToTop" />
    <Button Text="@Localizer["TextareasScrollToBottom"]" OnClick="ScrollToBottom" />
    <Button Text="@Localizer["TextareasScrollTo"]" OnClick="ScrollTo20" />
    <Button Text="@($"{Localizer["TextareasAutoScroll"]}{(IsAutoScroll ? " On":" Off")}" )" OnClick="SwitchAutoScroll" />
</BootstrapInputGroup>

@code {
    [NotNull]
    private Textarea? TextareaElement { get; set; }

    private string? Text { get; set; }

    private int ScrollValue { get; set; }

    private bool IsAutoScroll { get; set; }

    private CancellationTokenSource? CancelTokenSource { get; set; }

    private int Index { get; set; } = 5;

    /// <summary>
    /// <inheritdoc/>
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();

        for (int i = 0; i < Index; i++)
        {
            Text += $"{((i % 2 == 0) ? "A" : "B")} : {Localizer["TextareasMockChat"]}{i}{Environment.NewLine}";
        }
    }

    private Task ScrollToTop() => TextareaElement.ScrollToTop();

    private Task ScrollToBottom() => TextareaElement.ScrollToBottom();

    private Task ScrollTo20() => TextareaElement.ScrollTo(ScrollValue += 20);

    private void SwitchAutoScroll() => IsAutoScroll = !IsAutoScroll;

    private bool IsRunMock { get; set; }

    private string ChatLocalizerName => IsRunMock ? "TextareasMockChatStop" : "TextareasMockChatRun";

    /// <summary>
    /// MockChat
    /// </summary>
    private void MockChat()
    {
        if (CancelTokenSource != null)
        {
            CancelTokenSource.Cancel();
            CancelTokenSource.Dispose();
            CancelTokenSource = null;
        }

        IsRunMock = !IsRunMock;

        if (IsRunMock)
        {
            Task.Run(async () =>
            {
                CancelTokenSource ??= new();
                while (CancelTokenSource != null && !CancelTokenSource.IsCancellationRequested)
                {
                    Text += $"{((Index % 2 == 0) ? "A" : "B")} : {Localizer["TextareasMockChat"]}{Index}{Environment.NewLine}";
                    Index++;
                    await InvokeAsync(StateHasChanged);

                    try
                    {
                        if (CancelTokenSource != null)
                        {
                            await Task.Delay(500, CancelTokenSource.Token);
                        }
                    }
                    catch (TaskCanceledException) { }
                }
            });
        }
    }

    /// <summary>
    /// Dispose
    /// </summary>
    /// <param name="disposing"></param>
    private void Dispose(bool disposing)
    {
        if (disposing && CancelTokenSource != null)
        {
            CancelTokenSource.Cancel();
            CancelTokenSource.Dispose();
            CancelTokenSource = null;
        }
    }

    /// <summary>
    /// Dispose
    /// </summary>
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}
